<?php
/**
 * Created by PhpStorm.
 * User: 阿沁
 * Date: 2024/5/7
 * Time: 下午1:45
 * SOURCE: 中国大陆工时规管现况（标准工时）：
 *              一天工作时间为8小时，平均每周工时不超过40小时;
 *              加班上限为一天3小时及一个月36小时，逾时工作薪金不低于平日工资的150％。
 *              而一周最高工时则为48小时。平均每月计薪天数为21.75天。
 * TO: 我是阿沁,健康撸代码,拒绝通宵。
 * 版权所有: © 2024-2030 阿沁
 * WARNING: 未经本人许可, 禁止一切商用
 * CLASS: < TaskController.php >
 */

namespace app\controller;

use app\BaseController;
use app\common\UserService;
use app\model\GoodsOrders;
use app\model\ShopOrders;
use app\model\SuperOrders;
use app\model\SuperOrdersBill;
use app\model\Test;
use app\model\User;
use app\model\UserMoneyLog;

class TaskController extends BaseController
{
	
	#分红  10天一次
	function fenhong()
	{
		try {
            $test = Test::find(1);

			$total_amount = GoodsOrders::whereIn('status', [2, 3, 4, 5])->where('createtime','>=',$test->order_updatetime)->sum('pay_amount'); #近10天业绩总和
			#代理分红-------------------
			#县代理
			$xianuser = User::where('level', '>=', 2)->select();
			if (!$xianuser->isEmpty()) {
                $money = $total_amount * 0.06 / $xianuser->count();
				foreach ($xianuser as $user) {
                    if ($user->goods_quota > 0) {
                        if ($money > $user->goods_quota) {
                            $money = $user->goods_quota;
                        }
                        UserService::changeUserGoodsQuota(-$money, $user->id, '县代理分红扣除');
                        UserService::changeUserMoney($money, $user->id, '县代理分红');
                    }

				}
			}
			
			
			#区代理
			$quuser = User::where('level', '>=', 3)->select();
			if (!$quuser->isEmpty()) {
                $money = $total_amount * 0.06 / $quuser->count();
				foreach ($quuser as $user) {
                    if ($user->goods_quota > 0) {
                        if ($money > $user->goods_quota) {
                            $money = $user->goods_quota;
                        }
                        UserService::changeUserGoodsQuota(-$money, $user->id, '区代理分红扣除');
                        UserService::changeUserMoney($money, $user->id, '区代理分红');
                    }
				}
			}
			
			#市代理
			$shiuser = User::where('level', '>=', 4)->select();
			if (!$shiuser->isEmpty()) {
                $money = $total_amount * 0.06 / $shiuser->count();
				foreach ($shiuser as $user) {
                    if ($user->goods_quota > 0) {
                        if ($money > $user->goods_quota) {
                            $money = $user->goods_quota;
                        }
                        UserService::changeUserGoodsQuota(-$money, $user->id, '市代理分红扣除');
                        UserService::changeUserMoney($money, $user->id, '市代理分红');
                    }


				}
			}
			
			#省代理
			$shenguser = User::where('level', '>=', 5)->select();
			if (!$shenguser->isEmpty()) {
                $money = $total_amount * 0.06 / $shenguser->count();
				foreach ($shenguser as $user) {
                    if ($user->goods_quota > 0) {
                        if ($money > $user->goods_quota) {
                            $money = $user->goods_quota;
                        }
                        UserService::changeUserGoodsQuota(-$money, $user->id, '省代理分红扣除');
                        UserService::changeUserMoney($money, $user->id, '省代理分红');
                    }


				}
			}
			
			#总代理
			$zonguser = User::where('level', '>=', 6)->select();
			if (!$zonguser->isEmpty()) {
                $amount = $total_amount * 0.06 / $zonguser->count();
				foreach ($zonguser as $user) {
                    if ($user->goods_quota > 0) {
                        if ($amount > $user->goods_quota) {
                            $amount = $user->goods_quota;
                        }
                        UserService::changeUserGoodsQuota(-$amount, $user->id, '总代理扣除');
                        UserService::changeUserMoney($amount, $user->id, '总代理分红');
                    }
				}
			}
			
			
			#精品区分红--------------
			$goods_devote = User::sum('goods_devote');
			$amount = $total_amount * 0.15 / $goods_devote;
			$users = User::select();
			foreach ($users as $user) {
				if ($user->goods_devote > 0 && $user->goods_quota > 0) {
					$money = $amount * $user->goods_devote;
					if ($money > $user->goods_quota) {
						$money = $user->goods_quota;
					}
					UserService::changeUserGoodsQuota(-$money, $user->id, '精品区分红扣除');
					UserService::changeUserMoney($money, $user->id, '精品区分红');
				}
				
				#共富奖-------------
				$total_money = UserMoneyLog::where(['user_id' => $user->id])
					->where('money','>',0)
					->whereIn('memo', ['精品区推荐奖', '精品区分红', '县代理分红', '区代理分红', '市代理分红', '省代理分红', '总代理分红'])
					->whereTime('createtime', '-7 day')
					->sum('money');
				$item_count = $user->child()->count();
				$money = $total_money * 0.1 / $item_count; #平均分的金额
				#分给所有下级
				foreach ($user->child as $team) {
                    if ($team->first_buy_goods_time){
                        if ($team->goods_quota > 0) {
                            if ($amount > $team->goods_quota) {
                                $amount = $team->goods_quota;
                            }
                            UserService::changeUserGoodsQuota(-$money, $team->id, '共富奖分红扣除');
                            UserService::changeUserMoney($money, $team->id, '共富奖');
                        }

                    }
				}
			}
			
			#优选区分红
			$total_amount = SuperOrders::whereIn('status', [4, 5])->where('createtime','>=',$test->order_updatetime)->sum('pay_amount'); #近10天业绩总和
			$total_super_quota = User::sum('super_quota');
			if ($total_super_quota > 0 && $total_amount > 0) {
				$amount = $total_amount * 0.09 / $total_super_quota;
				$users = User::where('super_quota', '>', 0)->select();
				foreach ($users as $user) {
					$money = $user->super_quota * $amount;
					UserService::changeUserMoney($money, $user->id, '优选区分红');
					UserService::changeUserSuperQuota(-$money, $user->id, '优选区分红扣除');
				}
			}
			
			#门店分红
			$total_amount = ShopOrders::where('status',2)->where('createtime','>=',$test->order_updatetime)->sum('pay_amount');
			$total_shop_quota = User::sum('shop_quota');
			if ($total_shop_quota > 0 && $total_amount > 0) {
				$amount = $total_amount * 0.4 / $total_shop_quota;
				$users = User::where('shop_quota', '>', 0)->select();
				foreach ($users as $user) {
					$money = $user->shop_quota * $amount;
					UserService::changeUserMoney($money, $user->id, '商家额度分红');
					UserService::changeUserShopQuota(-$money, $user->id, '商家分红扣除');
				}
			}


            $test->order_updatetime = time();
            $test->save();

		} catch (\Throwable $e) {
			$this->writeJson(0, $e->getMessage());
		}
		$this->writeJson();
	}

    function bill()
    {
        $today_supply_price = SuperOrders::whereTime('paytime','yesterday')->where('status', 2)->sum('supply_price');
        $today_after_supply_price = SuperOrders::whereTime('aftertime','yesterday')->where('status', 10)->sum('supply_price');
        SuperOrdersBill::create([
            'today_supply_price'=>$today_supply_price,
            'today_after_supply_price'=>$today_after_supply_price,
            'day'=> date('Y-m-d', strtotime('-1 day')),
        ]);
        $this->writeJson();
    }
	
	
}